﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TTLL.Asm
{
    public class FromBin
    {
        public static bool Read5BitTypeAndData(System.IO.BinaryReader reader, out byte type, out UInt16 length, out byte[] readdata)
        {
            var data = reader.ReadByte();
            type = (byte)(data >> 3);
            var len = data & 0x07;
            switch (len)
            {
                case 1: length = 1; break;
                case 2: length = 2; break;
                case 3: length = 4; break;
                case 4: length = 8; break;
                case 5: length = 16; break;
                case 6: length = reader.ReadByte(); break;
                case 7: length = reader.ReadUInt16(); break;
                default: length = 0; break;
            }
            if (len > 0)
                readdata = reader.ReadBytes(length);
            else
                readdata = null;
            return true;
        }
        public static TTLL.Asm.Module LoadModule(byte[] data)
        {
            TTLL.Asm.Module module = new Module();

            using (var ms = new System.IO.MemoryStream(data))
            {
                using (var reader = new System.IO.BinaryReader(ms))
                {
                    module.ReadAll(reader);
                }
            }
            return module;
        }
    }
}
